/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * @author Administrator
 */
public class ChooseGameScene extends Scene {

    private static final int NUMBER_GAMES_PER_PAGE = 6;
    private static final int NUMBER_GAME = 8;
    private int iconGameStartIndex;
    private int numberGameShows;
    private int selectIconIndex;
    private int pageIndex;
    private int topUserIndex;
    private int friendIndex;
    //---------------------------------------------------
    private Image topBarImage;
    private Image bottomBarImage;
    private Image chandelierImage;
    private Image iconGameImages;
    private Image dotImages;
    private boolean isLock;
    protected FlyString adFlyString;
    //------------------------------------------------------
//    private String[] nameGames = {"Phỏm", "Xèng", "Tiến lên", "Ba cây", "Cờ vua", "Cửa hàng"};
    private int messageIndex;
    private int pointerDragX;
    private int pointerDragY;
    private boolean isPointerDrag;
    private int pointerPressX;
    private int poiterPressY;

    public ChooseGameScene(MainCanvas parent) {
        super(parent);
        numberGameShows = NUMBER_GAMES_PER_PAGE;
        initValue();
        initObject();

    }

    private void initValue() {
        isGetListFriend = false;
        dialogString = gameData.dialogString;
        gameData.dialogString = "";

    }

    private void initObject() {
        try {
            adFlyString = new FlyString(FlyString.FLY_HORIZONTAL, PiPoDesigner.COLOR_WHITE);
            adFlyString.setText(gameData.setAdString,
                    3, canvasWidth, topScreen + 5);
            adFlyString.show(10000);
            adFlyString.setLimitHorizontal(canvasWidth);
            initImage();
        } catch (IOException ex) {
        }
    }

    private void initImage() throws IOException {
        bottomBarImage = library.getBottomBarImage();
        topBarImage = library.getTopBarImage();
        chandelierImage = Utils.loadImage("den_chum.png");
        iconGameImages = Utils.loadImage("icon_game.png");
        dotImages = Utils.loadImage("dot.png");
    }

    public void setListSubMenu() {
        subMenu.setList(new String[]{"Thông tin cá nhân", "Hộp thư", "Bạn bè", "Top đại gia", "Top cao thủ"});
    }
    //------------------------------------------------------------------------

    public void doLoop() {
        super.doLoop();
        if (isShowReply) {
            replyBox.run();
            parent.repaint();
        }
        if (isShowBox) {
            nameChildBox.run();
            parent.repaint();
        }
        if (adFlyString != null) {
            if (adFlyString.isShow) {
                adFlyString.run();
                speakFlyString.isShow = false;
                parent.repaint();
            } else {
                adFlyString.show(10000);
                parent.repaint();
            }
        }
    }

    public void paintComponents(Graphics g) {
        paintBarMenu(g);
        if (!isLock) {
            paintIconGame(g);
            paintPaging(g);
        } else {
            paintLockIcon(g);
        }
    }

    private int getIconId(int id) {
        switch (id) {
            case 0:
                return 1;
            case 1:
                return 4;
            case 2:
                return 5;
            case 3:
                return 6;
            default:
                return 7;
        }
    }

    public void paintLockIcon(Graphics g) {
        int iconWidth = iconGameImages.getWidth() / NUMBER_GAME;
        int iconHeight = iconGameImages.getHeight();
        int paddingWidth = (canvasWidth - 3 * iconWidth) / 4;
        int paddingHeight = (canvasHeight - 2 * iconHeight) / 3;
        int topIcons = paddingHeight;
        int leftIcons = paddingWidth;
        for (int i = 0; i < 5; i++) {
            if (i % 3 == 0 && i != 0) {
                topIcons += 30 + iconHeight;
                leftIcons = paddingWidth;
            }
            Utils.drawFrame(g, iconGameImages, leftIcons, topIcons, getIconId(i), 0, iconWidth, iconHeight);
            if (i == selectIconIndex) {
                g.drawRoundRect(leftIcons, topIcons, iconWidth, iconHeight, 5, 5);
            }
            leftIcons += paddingWidth + iconWidth;
        }
    }

    private void paintPaging(Graphics g) {
        if (numberGameShows < NUMBER_GAME) {
            if (screenMode == SCREEN_PORTRAIT) {
                Utils.drawFrame(g, dotImages, canvasWidth / 2 - 10, canvasHeight - bottomBarHeight - 15, pageIndex, 0, dotImages.getWidth() / 2, dotImages.getHeight());
                Utils.drawFrame(g, dotImages, canvasWidth / 2 + 10, canvasHeight - bottomBarHeight - 15, 1 - pageIndex, 0, dotImages.getWidth() / 2, dotImages.getHeight());
            } else {
                Utils.drawFrame(g, dotImages, canvasWidth / 2 - 10, canvasHeight - bottomBarHeight + 5, pageIndex, 0, dotImages.getWidth() / 2, dotImages.getHeight());
                Utils.drawFrame(g, dotImages, canvasWidth / 2 + 10, canvasHeight - bottomBarHeight + 5, 1 - pageIndex, 0, dotImages.getWidth() / 2, dotImages.getHeight());
            }
        }
    }

    protected void paintBarMenu(Graphics g) {
        int anchorX = canvasWidth / 2 - chandelierImage.getWidth() / 2;
        int anchorY = 23;
        g.drawImage(chandelierImage, anchorX, anchorY, 0);
        g.drawImage(chandelierImage, anchorX - chandelierImage.getWidth() / 2, anchorY, 0);
        g.drawImage(chandelierImage, anchorX + chandelierImage.getWidth() / 2, anchorY, 0);
        g.drawImage(topBarImage, leftScreen, topScreen, 0);
        g.drawImage(bottomBarImage, leftScreen, canvasHeight - bottomBarImage.getHeight(), 0);
        PiPoDesigner.drawString(g, "Chọn Game", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth / 2 - 35, 5);
        if (isShowReply) {
            PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
            PiPoDesigner.drawString(g, "Xóa", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
            PiPoDesigner.drawString(g, "Gửi", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 15, canvasHeight - 25);
        } else if (isShowBox) {
            PiPoDesigner.drawString(g, "Xóa", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
            PiPoDesigner.drawString(g, "Ok", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 15, canvasHeight - 25);
        } else {
            super.paintBarMenu(g);
        }
        if (adFlyString != null) {
            if (adFlyString.isShow) {
                adFlyString.paint(g);
            }
        }

    }

    private void paintIconGame(Graphics g) {
        int iconWidth = iconGameImages.getWidth() / NUMBER_GAME;
        int iconHeight = iconGameImages.getHeight();
        int paddingWidth = (canvasWidth - 3 * iconWidth) / 4;
        int paddingHeight = (canvasHeight - 2 * iconHeight) / 3;
        int topIcons = paddingHeight;
        int leftIcons = paddingWidth;
        for (int i = iconGameStartIndex % NUMBER_GAMES_PER_PAGE; i < numberGameShows; i++) {
            if (i % 3 == 0 && i != 0) {
                topIcons += 30 + iconHeight;
                leftIcons = paddingWidth;
            }
            Utils.drawFrame(g, iconGameImages, leftIcons, topIcons, i + iconGameStartIndex, 0, iconWidth, iconHeight);
            if (i == selectIconIndex) {
                g.drawRoundRect(leftIcons, topIcons, iconWidth, iconHeight, 5, 5);
            }
            leftIcons += paddingWidth + iconWidth;
        }
    }
    //----------------------------------------------------------------------------

    public void doKeyLeft() {
        if (isShowTableSms && screenMode == SCREEN_LANDSCAPE) {
            charMoneyIndex = (charMoneyIndex < 3 ? 3 : 0);
            parent.repaint();
            return;
        }
        if (isShowMessage) {
            return;
        }
        if (isShowUserInfo) {
            moveLeftInfo();
            return;
        }
        selectIconIndex--;
        if (selectIconIndex == 2 || selectIconIndex < 0) {
            selectIconIndex += 3;
            selectIconIndex = 0;
            if (numberGameShows < NUMBER_GAME && !isLock) {
                changePage();
            }
        }
        parent.repaint();
    }

    public void doKeyRight() {
        if (isShowTableSms && screenMode == SCREEN_LANDSCAPE) {
            charMoneyIndex = (charMoneyIndex < 3 ? 3 : 0);
            parent.repaint();
            return;
        }
        if (isShowMessage) {
            return;
        }
        if (isShowUserInfo) {
            moveRightInfo();
            return;
        }
        selectIconIndex++;
        Debug.d("select :" + selectIconIndex);
        if (selectIconIndex % 3 == 0 || (iconGameStartIndex + selectIconIndex) == NUMBER_GAME) {
            selectIconIndex = 0;
            if (!isLock) {
                changePage();
            }
        }
        parent.repaint();
    }

    protected void doKeyUpRemain() {
        selectIconIndex -= 3;
        if (selectIconIndex < 0) {
            selectIconIndex += numberGameShows;
            if (selectIconIndex < 0) {
                selectIconIndex = 0;
            }

        }
        Debug.d("select :" + selectIconIndex);
        parent.repaint();
    }

    protected void doKeyDownRemain() {
        selectIconIndex += 3;
        if (selectIconIndex >= numberGameShows) {
            selectIconIndex -= numberGameShows;
            if (selectIconIndex >= numberGameShows) {
                selectIconIndex = numberGameShows - 1;
            }
        }
        //Debug.d("select:" + selectIconIndex);
        parent.repaint();
    }

    protected void doSoftKeyLeftRemain() {
    }

    protected void doSoftKeyRightRemain() {
        showLogin();
        parent.repaint();
    }

    protected void doKeyCenterRemain() {
        if (!isLock) {
            chosseGame();
        } else {
            chooseGameLock();
        }
        parent.repaint();
    }

    private void chooseGameLock() {
        Debug.d("selected :" + selectIconIndex);
        switch (selectIconIndex) {
            case 0://xeng
                isLoading = true;
                parent.displayLuckyCoin();
                break;
            case 1://co vua
                isLoading = true;
                Request.chosseGame(Constants.CO_VUA);
                parent.repaint();
                break;
            case 2:
                //shop
                isLoading = true;
                Request.chosseGame(Constants.CUA_HANG);
                break;
            case 3:
                isLoading = true;
                Request.getUserInfo();
                isGetUserInfo = true;
                isLoading = true;
                parent.repaint();
                break;
            case 4:
                charMoneyIndex = 0;
                isShowTableSms = true;
                parent.repaint();
                break;
//            case Constants.XENG:

//            case Constants.INFO:

//            case Constants.NAP_TIEN:

        }
    }

    private void changePage() {
        pageIndex = 1 - pageIndex;
        if (pageIndex == 0) {
            numberGameShows = NUMBER_GAMES_PER_PAGE;
            iconGameStartIndex = 0;
        } else {
            numberGameShows = NUMBER_GAME - NUMBER_GAMES_PER_PAGE;
            iconGameStartIndex = 6;
        }
        Debug.d("number show :" + numberGameShows);
        parent.repaint();
    }

    protected void doSubMenu() {
        if (isShowMessage) {
            doMenuMessage();
            return;
        }
        if (isShowUserInfo) {
            doUserInfo();
            return;
        }
        isShowSubMenu = false;
        parent.repaint();
        switch (subMenu.getSelectedIndex()) {
            case 0:
                getMyinfo();
                break;
            case 1:
                getMyMessage();
                break;
            case 2:
                Request.getListItem();
                Request.getUserInfo();
                Request.getListFriend();
                isGetListFriend = true;
                isLoading = true;
                parent.repaint();
                //ban be
                break;
            case 3:
                //top dai gia
                Request.getTopRich();
                isLoading = true;
                parent.repaint();
                break;
            case 4:
                //top cao thu
                Request.getTopLevel();
                isLoading = true;
                break;
        }
    }

    private void chosseGame() {
        gameData.setGameTypePlay(selectIconIndex + iconGameStartIndex);
        switch (selectIconIndex + iconGameStartIndex) {
            case Constants.CO_VUA:
                isLoading = true;
                Request.chosseGame(Constants.CO_VUA);
                break;
            case Constants.CUA_HANG:
                isLoading = true;
                Request.chosseGame(Constants.CUA_HANG);
                parent.repaint();
                break;
            case Constants.PHOM:
                isLoading = true;
                Request.chosseGame(Constants.PHOM);
                break;
            case Constants.BA_CAY:
                isLoading = true;
                Request.chosseGame(Constants.BA_CAY);
                break;
            case Constants.TIEN_LEN:
                isLoading = true;
                Request.chosseGame(Constants.TIEN_LEN);
                break;
            case Constants.XENG:
                isLoading = true;
                parent.displayLuckyCoin();
                break;
            case Constants.INFO:
                isLoading = true;
                Request.getUserInfo();
                isGetUserInfo = true;
                isLoading = true;
                parent.repaint();
                break;
            case Constants.NAP_TIEN:
                charMoneyIndex = 0;
                isShowTableSms = true;
                parent.repaint();
                break;
        }
    }
    //------------------------------------------------------------------------

    public void inputKeyCode(int key) {
        if (isShowReply) {
            replyBox.inputChar(key);
            parent.repaint();
        }
        if (isShowBox) {
            nameChildBox.inputChar(key);
            parent.repaint();
        }
        if (dialog != null) {
            if (dialog.isShow) {
                dialog.input(key);
                parent.repaint();
                return;
            }
        }
    }

//    protected void touchPressedRemain(int x, int y) {
//        touchIconGame(x, y);
//    }
    private void checkPointerDrag() {
        if (pointerPressX - 8 < pointerDragX && pointerDragX < pointerPressX + 8
                && poiterPressY - 8 < pointerDragY && pointerDragY < poiterPressY + 8) {
            isPointerDrag = false;
        }
    }

    protected void touchPressedRemain(int x, int y) {
        isPointerDrag = false;
        pointerPressX = x;
        poiterPressY = y;
    }

    public void doTouchReleased(int x, int y) {
        checkPointerDrag();
        if (isPointerDrag) {
            if (x - pointerPressX < 0) {
                Debug.d("move left");
                doKeyLeft();
                return;
            }
            if (x - pointerPressX > 0) {
                doKeyRight();
                Debug.d("move right");
                return;
            }
        } else {
            if (isShowTableSms) {
                touchTableSms(x, y);
                doTableSms();
                return;
            }
            touchIconGame(x, y);
        }
    }

    private void touchTableSms(int x, int y) {
//        int left = leftScreen + 10;
        int top = 30;
//        left += 10;
        top += 35;
        if (top < y && y < top + 30) {
            charMoneyIndex = 0;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 1;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 2;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 2;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 3;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 4;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 5;
            return;
        }
    }

    public void doTouchDraged(int x, int y) {
        pointerDragX = x;
        pointerDragY = y;
        isPointerDrag = true;
    }

    private void touchIconGame(int touchX, int touchY) {
        int iconWidth = iconGameImages.getWidth() / 9;
        int iconHeight = iconGameImages.getHeight();
        int paddingWidth = (canvasWidth - 3 * iconWidth) / 4;
        int paddingHeight = (canvasHeight - 2 * iconHeight) / 3;
        int topIcons = paddingHeight;
        int leftIcons = paddingWidth;
        for (int i = iconGameStartIndex % NUMBER_GAMES_PER_PAGE; i < numberGameShows; i++) {
            if (i % 3 == 0 && i != 0) {
                topIcons += 30 + iconHeight;
                leftIcons = paddingWidth;
            }
            if (Utils.isTouch(leftIcons - 10, topIcons - 10, iconWidth + 20, iconHeight + 20, touchX, touchY)) {
                selectIconIndex = i;
                chosseGame();
//                Debug.d("select icon :" + i);
                parent.repaint();
                break;
            }
            leftIcons += paddingWidth + iconWidth;
        }
    }

//-------------------------------------------------------------------------------
    public void processMessage(String[] part) {
        super.processMessage(part);
        if (part[0].equals("GA")) {
            gameData.setGaData(part);
            showChooseRoom();
            return;
        }
        if (part[0].equals("TL")) {
            if (part[1].equals("1")) {
                gameData.setTopUserType(1);
                gameData.initTopUserInfo();
                return;
            }
            if (part[1].equals("-1")) {
                gameData.setNumberTopInfo(topUserIndex);
                topUserIndex = 0;
                showTopUser();
                return;
            }
            PlayerInfo playerInfo = new PlayerInfo(part[1], 0, Integer.parseInt(part[3]));
            gameData.setTopUserInfo(topUserIndex, playerInfo);
            topUserIndex++;
            return;
        }
        if (part[0].equals("TR")) {
            if (part[1].equals("1")) {
                gameData.setTopUserType(0);
                gameData.initTopUserInfo();
                return;
            }
            if (part[1].equals("-1")) {
                gameData.setNumberTopInfo(topUserIndex);
                topUserIndex = 0;
                showTopUser();
                return;
            }
            PlayerInfo playerInfo = new PlayerInfo(part[1], Long.parseLong(part[3]), 0);
            gameData.setTopUserInfo(topUserIndex, playerInfo);
            topUserIndex++;
            return;
        }
        if (part[0].equals("LF")) {
            //LF|name|online|name|online
            if (part[1].equals("1")) {
                friendIndex = 0;
                gameData.initListFriend(Integer.parseInt(part[2]));
                return;
            }
            if (part[1].equals("-1")) {
                showListFriend();
                isGetListFriend = false;
                return;
            }
            gameData.setFriendInfo(friendIndex, part[1], Integer.parseInt(part[2]), part[3],
                    Integer.parseInt(part[4]), Integer.parseInt(part[5]), Integer.parseInt(part[6]));
            friendIndex++;
        }

        if (part[0].equals("MO")) {
            gameData.setMyMoney(Long.parseLong(part[1]));
        }
        if (part[0].equals("ITEM")) {
            gameData.setItemArr(part);
        }
        if (part[0].equals("MYI")) {
            gameData.initShopData(part);
            if (!isGetListFriend) {
                showShopScene();
            }
        }

    }

    public void dispose() {
        Debug.d("choose game dipose");
        topBarImage = null;
        bottomBarImage = null;
        chandelierImage = null;
        iconGameImages = null;
        dotImages = null;
//        nameGames = null;
        super.dispose();
    }

    protected void reponseQuestion(int result) {
        if (isShowMessage) {
            responseMarry(result);
            return;
        }
    }

    public void doKeyPound() {
        if (dialog != null) {
            if (dialog.isShow) {
                dialog.doKeyPound();
                parent.repaint();
                return;
            }
        }
    }
}
